﻿using SisGrafeno;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using SisGrafeno;

namespace SisGrafenoController
{
    class SQLServerDAONotaCredito : DAONotaCredito
    {

        public void add(NotaCredito nc)
        {
            SqlConnection conn = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO BooxNotaCredito(id,montoTotal,fecha,motivo" +
                ") VALUES (@param1,@param2,@param3,@param4)";
                SqlParameter param1 = new SqlParameter("@param1",
                                                        System.Data.SqlDbType.Int);
                SqlParameter param2 = new SqlParameter("@param2",
                                                        System.Data.SqlDbType.DateTime);
                SqlParameter param3 = new SqlParameter("@param3",
                                                        System.Data.SqlDbType.Float);
                SqlParameter param4 = new SqlParameter("@param4",
                                                        System.Data.SqlDbType.VarChar);



                param1.Value = nc.getId();
                param2.Value = nc.getMontoTotal();
                param3.Value = nc.getFechaNota();
                param4.Value = nc.getMotivo();

                comm.Parameters.Add(param1);
                comm.Parameters.Add(param2);
                comm.Parameters.Add(param3);
                comm.Parameters.Add(param4);

                //Paso 4. Ejecutar la sentencia
                comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs

            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
        }
        public NotaCredito queryById(int notaCreditoId)
        {
            NotaCredito nc = null;
            SqlConnection conn = null;
            SqlDataReader myReader = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM BooxNotaCredito WHERE id=@param1";
                SqlParameter param1 = new SqlParameter("@param1",
                                                        System.Data.SqlDbType.Int);
                param1.Value = notaCreditoId;
                comm.Parameters.Add(param1);

                //Paso 4. Ejecutar la sentencia
                myReader = comm.ExecuteReader();

                //Paso 5: Evaluar los resultados
                if (myReader.Read())
                {
                    nc = new NotaCredito();
                    nc.setId(Int32.Parse(myReader["id"].ToString()));
                    nc.setMontoTotal(Double.Parse(myReader["montoTotal"].ToString()));
                    nc.setMotivo(myReader["motivo"].ToString());
                    nc.setFechaNota(DateTime.Parse(myReader["fechaNota"].ToString()) );
                }
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs
                throw e;
            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!!
                try { if (myReader != null) myReader.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
            return nc;
        }
    }
}
